<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>递归函数</title>
</head>

<body>
    <script>
        /* 
        递归函数：如果一个函数在内部可以调用其本身，那么这个函数就是递归函数。
        简单理解: 函数内部自己调用自己, 这个函数就是递归函数

        // 递归函数找到出口  函数结束    ，反向结束
        比如： 阶乘  1 * 2 * 3 * 4 *....* 10

        10 *  9   *  8   *..... * 1
        10(10 - 1)(9 - 1) *. . . .1
              9      8 
        */

        var num = 0;
        function show() {
            num++;
            if (num == 3) {
                return;
            }
            console.log('run');
            show();
        }
        show();

        // 案例1：求n的阶乘
        // function jieCheng(n) {
        //     var chengJi = 1;
        //     for (var i = 1; i <= n; i++) {
        //         chengJi *= i;
        //     }
        //     return chengJi;
        // }
        // var chengJi = jieCheng(10);
        // console.log(chengJi);//3628800



        //   递归方法来编程
        /* 
        10*9*8*.....*1
         10 (10-1)  (9-1) *.... 1
               9      8
          n*(n-1)
        */

        function jieCheng1(n) {
            if (n <= 0) {
                return n + '不能小于等于0'
            } else if (n == 1) {
                // 出口
                return 1;
            } else {
                return n * jieCheng1(n - 1)
            }
        }

        var chengJi1 = jieCheng1(5)
        /* 
        chengJi1=   5 * jieCheng(4)
                                     
                        4 * jieCheng(3)
                             3 * jieCheng(2)  
                                  2 * jieCheng(1)
                                         1
        */
        var chengJi1 = jieCheng1(10)
        console.log(chengJi1);  //3628800


        /* 
            案例2：  
            求斐波那契数列第N项的值 和前 N项的和
            n=0   n>=2   F(n)=F(n-2)+F(n-1)

            1 1 2 3 5 8 13 21... 
        */

        function feibonaqicci(n) {
            var f1 = 1;
            var f2 = 1;
            var f3;
            var sum = 2;
            for (var i = 2; i < n; i++) {
                f3 = f1 + f2;
                f1 = f2;
                f2 = f3;
                sum += f3;

            }
            console.log('f3=' + f3, 'sum=' + sum);//f3=21 sum=54

        }
        feibonaqicci(8)  


        // 递归
        function feibonaqicci(n){
            if(n<0){
                return n+'不能小于0'
            }else if(n==0){
                return 0;

            }else if(n==1||n==2){
                return 1;
             }else{
               return  feibonaqicci(n-2)+feibonaqicci(n-1) ;
             }
            
        }

    var f3=  feibonaqicci(8)
    console.log(f3);

    /* 
     var f3= feibonaqicci(6)+feibonaqicci(7)
              
            feibonaqicci(4)+feibonaqicci(5)+feibonaqicci(5)+feibonaqicci(6)

     feibonaqicci(2)+feibonaqicci(3) +feibonaqicci(3)+feibonaqicci(4) +feibonaqicci(3)+feibonaqicci(4)+  feibonaqicci(4)+feibonaqicci(5)

     1+feibonaqicci(1)+feibonaqicci(2)+feibonaqicci(1)+feibonaqicci(2)+feibonaqicci(2)+feibonaqicci(3)+feibonaqicci(1)+feibonaqicci(2)+....
         1                 1              1                   1              1                               1                1
    
    
    */

    // 前 n项的和
    function getSum(n){
         if(n<0){
            return n+'不能小于0'
         }else if(n==0){
             return 0;
         }else if(n==1){
            return 1;
         }else{
           return getSum(n-1)+feibonaqicci(n) 
         }     

    }
   var sum =  getSum(8);
   console.log(sum);//54
    </script>
</body>

</html>